home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / k_r.arc / K&R_CH6.C < prev    next >
Text File  |  1985-11-05  |  7KB  |  465 lines

  1. pr 6.*
  2.  
  3.  
  4. Jul 27 17:12 1984  6.14struct1.c Page 1
  5.  
  6.  
  7. /* demonstration of a structure, 1st version */
  8.  
  9. struct data
  10. { char first[20];
  11.   char  last[26];
  12.   double     sal;
  13. };                      /* external definition */
  14. main()                  /* print structure contents */
  15. { static struct data emp = { "Linda",
  16.                              "Sample",
  17.                               39000.0
  18.                            };   /* declare and init */
  19.   printf("%s %s earns $%.2f per year\n",
  20.                 emp.first, emp.last, emp.sal);
  21. }
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. Jul 27 17:12 1984  6.17struct2.c Page 1
  71.  
  72.  
  73. /* demonstration of a structure, 2nd version */
  74.  
  75. #define FSZ 20
  76. #define LSZ 26
  77. #include <stdio.h>
  78. #include "getlinepn.f"  /* does not retain newline */
  79. #include "getdbl.f"
  80. struct
  81. { char first[FSZ];
  82.   char  last[LSZ];
  83.   double     sal;
  84. } emp;  /* external definition and declaration */
  85. main()  /* populate then print structure */
  86. { double getdbl();
  87.   printf("input first name: "); getlinepn(emp.first, FSZ);
  88.   printf("input last name: ");  getlinepn(emp.last, LSZ);
  89.   printf("(annual salary) ");   emp.sal = getdbl();
  90.   printf("%s %s earns $%.2f per year\n",
  91.                 emp.first, emp.last, emp.sal);
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Jul 27 17:12 1984  6.20aos.c Page 1
  137.  
  138.  
  139. /* demonstration of an array of structures */
  140.  
  141. struct data
  142. { char first[20];
  143.   char  last[26];
  144.   double     sal;
  145. };              /* external definition */
  146. main()          /* find and print last names M - Z */
  147. { register i;
  148.   static struct data ar[] = { { "Linda", "Sample", 39000.0 },
  149.                               { "Sam", "Next", 37500.0 },
  150.                               { "Larry", "Last", 34900.0 },
  151.                              }; /* declare and init array ar */
  152.   printf("employees with last names M - Z...\n");
  153.   for (i = 0; i < (sizeof(ar) / sizeof(ar[0])); ++i)
  154.     if (ar[i].last[0] >= 'M' && ar[i].last[0] <= 'Z')
  155.         printf("\t%s %s earns $%.2f per year\n",
  156.           ar[i].first, ar[i].last, ar[i].sal);
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. Jul 27 17:12 1984  6.24cbv.c Page 1
  203.  
  204.  
  205. /* passing structures as arguments - by value */
  206.  
  207. struct data
  208. { char first[20];
  209.   char  last[26];
  210.   double     sal;
  211. };              /* external definition */
  212. main()          /* test check() */
  213. { register i;
  214.   double pay = 36000.0;
  215.   static struct data ar[] = { { "Linda", "Sample", 39000.0 },
  216.                               { "Sam", "Next", 37500.0 },
  217.                               { "Larry", "Last", 34900.0 },
  218.                              }; /* declare and init array ar */
  219.   printf("employees earning more than $%.2f per year...\n", pay);
  220.   for (i = 0; i < (sizeof(ar) / sizeof(ar[0])); ++i)
  221.     if (check(ar[i], pay))
  222.         printf("\t%s %s earns $%.2f per year\n",
  223.           ar[i].first, ar[i].last, ar[i].sal);
  224. }
  225. check(str, lim)         /* return 1 if sal >= lim */
  226. struct data str;        /* struct declaration */
  227. double lim;
  228. { if (str.sal >= lim)
  229.         return (1);
  230.   return (0);
  231. }
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. Jul 27 17:12 1984  6.26cbr.c Page 1
  269.  
  270.  
  271. /* passing structures as arguments - by reference */
  272.  
  273. struct data
  274. { char first[20];
  275.   char  last[26];
  276.   double     sal;
  277. };              /* external definition */
  278. main()          /* test check_p() */
  279. { register i;
  280.   double pay = 36000.0;
  281.   static struct data ar[] = { { "Linda", "Sample", 39000.0 },
  282.                               { "Sam", "Next", 37500.0 },
  283.                               { "Larry", "Last", 34900.0 },
  284.                              }; /* declare and init array ar */
  285.   printf("employees earning more than $%.2f per year...\n", pay);
  286.   for (i = 0; i < (sizeof(ar) / sizeof(ar[0])); ++i)
  287.     if (check_p(&ar[i], pay))
  288.         printf("\t%s %s earns $%.2f per year\n",
  289.           ar[i].first, ar[i].last, ar[i].sal);
  290. }
  291. check_p(pstr, lim)      /* return 1 if sal >= lim */
  292. struct data *pstr;      /* pointer to struct declaration */
  293. double lim;
  294. { if (pstr->sal >= lim)
  295.         return (1);
  296.   return (0);
  297. }
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. Jul 27 17:12 1984  6.34fields.c Page 1
  335.  
  336.  
  337. /* structure with fields */
  338.  
  339. #define YES 1
  340. #define NO  0
  341. struct
  342. { char first[20];
  343.   char  last[26];
  344.   double     sal;
  345.   unsigned retire : 1;  /* field width 1 bit */
  346.   unsigned school : 1;  /* field width 1 bit */
  347. } ar[] = { { "Linda", "Sample", 39000.0, NO,  NO },
  348.            { "Sam",   "Next",   37500.0, NO, YES },
  349.            { "Larry", "Last",   34900.0, YES, NO },
  350.          };     /* declare and init external array */
  351. main()  /* find and print active employees;
  352.         /* note if in school */
  353. { register i;
  354.   printf("active employees...\n");
  355.   for (i = 0; i < (sizeof(ar) / sizeof(ar[0])); ++i)
  356.   { if (!ar[i].retire)
  357.     {   printf("\t%s %s earns $%.2f per year\n",
  358.            ar[i].first, ar[i].last, ar[i].sal);
  359.         if (ar[i].school)
  360.            printf("\t\t(in school)\n");
  361.     }
  362.   }
  363. }
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. Aug  7 15:11 1984  6.40unions.c Page 1
  401.  
  402.  
  403. union utag
  404. { char  ca[256];        /* type 1 */
  405.   int       num;        /* type 2 */
  406. };                      /* external definition */
  407. #include <stdio.h>
  408. #include "getline.f"
  409. #include "getint.f"
  410. main(argc, argv)        /* load union, call chk_u() */
  411. int argc;               /*      to test and print  */
  412. char **argv;
  413. { union utag udemo;     /* union declaration */
  414.   ++argv;               /* point to argv[1]  */
  415.   if (*++*argv != '\0') /* test 2nd char of *argv[1] */
  416.     chk_u(0, &udemo);   /*      if not '\0' fail    */
  417.   switch (*--*argv)     /* test 1st char of *argv[1] */
  418.   { case '1': printf("input line: "); getline(udemo.ca, 256);
  419.               chk_u(**argv, &udemo);
  420.     case '2': udemo.num = getint(); chk_u(**argv, &udemo);
  421.     default:  chk_u(**argv, &udemo);
  422.   }
  423. }
  424. chk_u(utype, pun)       /* test, then print union and exit */
  425. char utype;
  426. union utag *pun;        /* pun is pointer to union */
  427. if(pun -> ca == pun ->num)
  428.      printf("same size");
  429. else printf("different size");
  430. { switch(utype)
  431.   { case '1': printf("type 1, content...\n\t%s", pun->ca);
  432.               break;
  433.     case '2': printf("type 2, value is %d\n", pun->num);
  434.               break;
  435.     default:  printf("usage: filename { 1 2 }\n");
  436.               exit(1);  /* exit unsuccessfully */
  437.   }
  438.   exit(0);              /* exit successfully */
  439. }
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.